package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/13.<br><br>
 *
 * 丑数
 */
public class Topic263 {

    public static void main(String[] args) {
        Topic263 instance = new Topic263();

        System.out.println(instance.isUgly(6));
        System.out.println(instance.isUgly(8));
        System.out.println(instance.isUgly(14));
        System.out.println(instance.isUgly(1));
    }

    public boolean isUgly(int n) {
        if(n == 1) {
            return true;
        }

        if(n == 0) {
            return false;
        }

        while (n % 2 == 0) {
            n = n / 2;
        }

        while (n % 3 == 0) {
            n = n / 3;
        }

        while (n % 5 == 0) {
            n = n / 5;
        }

        return n == 1;
    }

    public boolean isUgly2(int n) {
        if(n == 1) {
            return true;
        }

        if(n == 0) {
            return false;
        }

        if(n % 2 == 0) return isUgly(n /2);
        if(n % 3 == 0) return isUgly(n / 3);
        if(n % 5 == 0) return isUgly(n / 5);

        return false;
    }
}
